package com.example.sharding;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class ShardingController {
    @Autowired(required = false)
    private JdbcTemplate jdbcTemplate;

    @PostMapping("/api/save")
    public String save(){
        String sql="insert into tb_order values (?,?,?,?,?,?,?)";
        jdbcTemplate.update(sql,new Object[]{2,1,2,"Spring",100,new Date(),new Date()});
        return "success";
   }

   @GetMapping("/api/selectAll")
   public List<Map<String,Object>>  selectAll(){
        String sql="select * from tb_order";
        List<Map<String,Object>> list=jdbcTemplate.query(sql, (rs, rowNum) -> {
            Map<String,Object> map=new HashMap<>();
            map.put("id",rs.getInt("id"));
            map.put("course_name",rs.getString("course_name"));
            // 设置其他属性
            return map;
        });
       return list;
    }

}
